package com.lottery.ruok.lottrey.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.eventbusObject.ForgetPassword;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.ForgetPasswordRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.ForgetPasswordResponse;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import utils.Utils;

public class ForgetPasswordActivity4 extends BaseActivity {


    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.line2)
    ImageView line2;
    @BindView(R.id.next)
    TextView next;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password4);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(password.getText().length() == 0){
                    next.setEnabled(false);
                    next.setTextColor(Color.parseColor("#8539d1"));
                } else {
                    next.setEnabled(true);
                    next.setTextColor(Color.parseColor("#ffffff"));
                }
            }
        });
    }

    @OnClick(R.id.next)
    public void onViewClicked() {
        if(password.getText().length() < 6||password.getText().length() > 20){
            Utils.showToast("密码不能小于6位或大于20位");
            return;
        } else if(!Utils.hasDigit(password.getText().toString()) || !Utils.judgeContainsStr(password.getText().toString())){
            Utils.showToast("密码请包含字母和数字");
            return;
        }

        final String phones = getIntent().getStringExtra("phone");// 小韓在 step 1 返回的 "e164String_phone" 格式
        RetrofitBase.getRetrofit().create(ForgetPasswordRequest.class)
                .getResult(4, phones, null, null, password.getText().toString())
                .compose(RxHelper.<ForgetPasswordResponse>handleResult())
                .subscribe(new RxSubscribe<ForgetPasswordResponse>(this) {
                    @Override
                    protected void _onNext(ForgetPasswordResponse forgetPasswordResponse) {
                        if(forgetPasswordResponse.getStatus() == 1) {
                            Utils.showToast("密码修改成功");

                            EventBus.getDefault().post(new ForgetPassword());

                            finish();
                        }
                    }

                    @Override
                    protected boolean showDialog() {
                        return true;
                    }
                });
    }
}
